using System;
using System.Collections.Generic;
using System.Text;
using da = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.DataAccess;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common.DataSets;


namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business
{
    /// <summary>
    /// Provides Business Logic for ServiceProviderType operations, including CRUD.
    /// </summary>
    public class ServiceProviderTypeManager
    {

        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        public ServiceProviderTypeManager() { }

        #endregion

        #region Get/search methods
        /// <summary>
        /// Search for Service Provider Types based on search criteria.
        /// </summary>
        /// <param name="serviceProviderTypeSearchRequest">An object representing the search criteria.</param>
        /// <returns>Returns a list of Service Provider Types that match the search criteria.</returns>
        public static ServiceProviderTypes ServiceProviderTypeSearch(ServiceProviderTypeSearchRequest serviceProviderTypeSearchRequest)
        {
            ServiceProviderTypes serviceProviderTypes = new ServiceProviderTypes();
            dsProviderType ds = da.ServiceProviderType.ServiceProviderTypeSearch(serviceProviderTypeSearchRequest.DisplayName,
                serviceProviderTypeSearchRequest.ServiceProviderTypeCode, serviceProviderTypeSearchRequest.PoolID, serviceProviderTypeSearchRequest.InPool);

            foreach (dsProviderType.ServiceProviderTypeRow dr in ds.ServiceProviderType)
            {
                ServiceProviderType currentProviderType = new ServiceProviderType();
                currentProviderType.ServiceProviderTypeID = dr.serviceProviderTypeID;
                currentProviderType.ServiceProviderTypeCode = dr.serviceProviderTypeCode;
                currentProviderType.DisplayName = dr.displayName;                
                serviceProviderTypes.Items.Add(currentProviderType);
            }
            return serviceProviderTypes;
        }
        
        /// <summary>
        /// Gets a specific ServiceProviderType by ID
        /// </summary>
        /// <param name="serviceprovidertypeid">The ID of the service provider type to retrieve</param>
        /// <returns>A ServiceProviderType object representing the requested service provider type</returns>
        public static ServiceProviderType ServiceProviderTypeSelectItem(string serviceProviderTypeID)
        {
            ServiceProviderType currentServiceProviderType = new ServiceProviderType();
            dvRefData ds = da.ServiceProviderType.ServiceProviderTypeSelectItem(serviceProviderTypeID);
            dvRefData.ServiceProviderTypeRow dr = ds.ServiceProviderType[0];

            currentServiceProviderType.ServiceProviderTypeID = dr.serviceProviderTypeID;
            currentServiceProviderType.DisplayName = dr.displayName;
            currentServiceProviderType.ServiceProviderTypeCode = dr.serviceProviderTypeCode;

            return currentServiceProviderType;
        }

        #endregion

        #region Insert/Update methods

        /// <summary>
        /// Inserts a service provider type
        /// </summary>
        /// <param name="serviceprovidertypeid">The service provider type to be inserted</param>
        /// <param name="userID">The user that has the object locked</param>
        /// <returns>The new ID value - empty if unsuccessful</returns>
        public static string ServiceProviderTypeInsert(ServiceProviderType serviceProviderType, string userID)
        {
            string newServiceProviderTypeID = string.Empty;

            if (da.ServiceProviderType.ServiceProviderTypeExists(serviceProviderType.DisplayName) == 0)
            {
                newServiceProviderTypeID = da.ServiceProviderType.ServiceProviderTypeInsert(serviceProviderType.DisplayName, serviceProviderType.ServiceProviderTypeCode);
                // set id to value returned from insert 
                serviceProviderType.ServiceProviderTypeID = int.Parse(newServiceProviderTypeID);
            }

            // return error if insert failed (i.e didn't return a new key
            //Work Item ID: 1600
            //Modified By: SachinJo
            //if(newServiceProviderTypeID == "") return "99";
            if (string.IsNullOrEmpty(newServiceProviderTypeID)) return "99";    

            // otherwise, return successful
            return "0";
        }

        /// <summary>
        /// Updates a service provider type
        /// </summary>
        /// <param name="serviceProviderType">The service provider type to be updated</param>
        /// <param name="userID">The user that has the object locked</param>
        /// <returns>A status code ("0" is successful)</returns>
        public static string ServiceProviderTypeUpdate(ServiceProviderType serviceProviderType, string userID)
        {
            // return error if object is not already checked out to provider
            if (!da.ServiceProviderType.ServiceProviderTypeIsLockedByUser(serviceProviderType.ServiceProviderTypeID.ToString(), userID)) return "10";
            
            int result = da.ServiceProviderType.ServiceProviderTypeExists(serviceProviderType.DisplayName);
            if (result == 0 || result == serviceProviderType.ServiceProviderTypeID)
            {
                // return error if can't update 
                if (!da.ServiceProviderType.ServiceProviderTypeUpdate(serviceProviderType.ServiceProviderTypeID.ToString(), serviceProviderType.DisplayName, serviceProviderType.ServiceProviderTypeCode))
                {
                    return "99";    // can't update
                }
            }
            else
            {
                return "99";
            }


            // otherwise, return successful
            return "0";
        }


   
        #endregion

        #region Delete methods

        /// <summary>
        /// Deletes a service provider type
        /// </summary>
        /// <param name="serviceprovidertypeid">The service provider type to be deleted</param>
        /// <param name="userID">The user that has the object locked</param>
        /// <returns>A status code ("0" is successful)</returns>
        public static string ServiceProviderTypeDelete(string serviceProviderTypeID, string userID)
        {            

            // return error if object is not already checked out to provider
            if (!da.ServiceProviderType.ServiceProviderTypeIsLockedByUser(serviceProviderTypeID, userID)) return "10";

            //return an error if any non deleted serviceProvider has this type associated with it
            //so run a spSearch - if any records are returned ....

            //TODO: update the ids to int
            //  uncomment
            //ServiceProviderSearchRequest spSearch = new ServiceProviderSearchRequest("", serviceProviderTypeID, "");
            //ServiceProviders providers = ServiceProviderManager.ServiceProviderSearch(spSearch);
            //if (providers.Items.Count != 0)
            //    return "11";

            // return error if can't delete links
            if (!da.PoolServiceProviderType.PoolServiceProviderTypeDeleteByServiceProviderTypeID(serviceProviderTypeID)) return "99";

            //delete the relationship between spt and msgtype:
            if (!da.ServiceProviderTypeMessageType.ServiceProviderTypeMessageTypeDeleteByServiceProviderTypeID(serviceProviderTypeID)) return "99";

            // return error if can't delete 
            if (!da.ServiceProviderType.ServiceProviderTypeDelete(serviceProviderTypeID)) return "99";    // can't delete

            // otherwise, return successful
            return "0";
        }

        #endregion

        #region Check in/out methods

        /// <summary>
        /// Checks out a service provider type
        /// </summary>
        /// <param name="serviceprovidertypeid">The service provider type to be checked out</param>
        /// <param name="userID">The user that is locking the object</param>
        /// <returns>A status code ("0" is successful)</returns>
        public static string ServiceProviderTypeLock(string serviceProviderTypeID, string userID)
        {
            // check if record is already locked, if it is then check to see if the current user already has it locked
            if (da.ServiceProviderType.ServiceProviderTypeIsLocked(serviceProviderTypeID))
            {
                if (da.ServiceProviderType.ServiceProviderTypeIsLockedByUser(serviceProviderTypeID, userID))
                    return "0";
                else
                    return "9";
            }
            // return error if check out fails
            if (!da.ServiceProviderType.ServiceProviderTypeLock(serviceProviderTypeID, userID)) return "99";

            // otherwise, return successful
            return "0";
        }


        /// <summary>
        /// Checks in a service provider type
        /// </summary>
        /// <param name="serviceprovidertypeid">The service provider type that is checked out</param>
        /// <param name="userID">The user that has the object locked</param>
        /// <returns>A status code ("0" is successful)</returns>
        public static string ServiceProviderTypeUnlock(string serviceProviderTypeID, string userID)
        {
            // return error if object is not already checked out to provider
            if (!da.ServiceProviderType.ServiceProviderTypeIsLockedByUser(serviceProviderTypeID, userID)) return "10";

            // return error if check in -fails fails
            if (!da.ServiceProviderType.ServiceProviderTypeUnlock(serviceProviderTypeID)) return "99";

            // otherwise, return successful
            return "0";
        }


        /// <summary>
        /// Unlocks all ServiceProviderTypes locked by a specific user
        /// </summary>
        /// <param name="userID">The user that has the objects locked</param>
        /// <returns>A status code ("0" is successful)</returns>
        public static string ServiceProviderUnlockAll(string userID)
        {
            // return error if check in -fails fails
            if (!da.ServiceProviderType.ServiceProviderTypeUnlockAll(userID)) return "99";

            // otherwise, return successful
            return "0";
        }


       

        #endregion

    }

}
